//
//  WYMediaLibraryController.m
//  CirclePush
//
//  Created by admin on 2018/4/10.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMediaLibraryController.h"
#import "WYRecommendCell.h"
#import "WYMediaTitleView.h"
#import "WYMdeiaCell.h"
#import "WYConditionView.h"
#import "WYAttribute.h"
#import "WYUpModel.h"
#import "WYPriceModel.h"
#import "WYMediaDetailController.h"
#import "WYSearchReusltController.h"
#import "TLCityPickerController.h"




@interface WYMediaLibraryController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,WYMediaTitleViewDelegate,UITextFieldDelegate,TLCityPickerDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *categoryView;
@property (nonatomic,strong)WYMediaTitleView *titleView;
@property (nonatomic,strong)UISearchBar *searchBar;
@property (nonatomic,strong)NSMutableArray *headDataArray;

@property (nonatomic,strong)NSMutableArray *collArray;
@property (nonatomic,strong)WYConditionView *conditionView;

@property (nonatomic,strong)NSMutableArray *upArray;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSString *typeId;
@property (nonatomic,strong)NSArray *priceArray;
@property (nonatomic,strong)NSMutableArray *cityArray;

@property (nonatomic,assign)WYHot hot;

@property (nonatomic,strong)WYMediaType *NocmediaType;
@property (nonatomic)BOOL isNoc;

@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,assign)BOOL refresh;
@property (nonatomic,assign)NSUInteger page;


@end

@implementation WYMediaLibraryController
{
    NSUInteger _index;
}
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(NSMutableArray *)upArray
{
    if (!_upArray) {
        if (!_upArray) {
            _upArray = [[NSMutableArray alloc]init];
        }
    }
    return _upArray;
}
-(NSMutableArray *)collArray
{
    if (!_collArray) {
        _collArray = [[NSMutableArray alloc]init];
    }
    return _collArray;
}
-(NSMutableArray *)headDataArray
{
    if (!_headDataArray) {
        _headDataArray = [[NSMutableArray alloc]init];
    }
    return _headDataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.hot = WYHotNormal;
    [self setupUI];
    [self loadListData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNoticfic:) name:@"selectMediaTypeName" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickHotMedia:) name:@"clickHotMedia" object:nil];
    WEAKSELF
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.refresh=NO;
        weakSelf.page++;
        [weakSelf loadListData];
       // [weakSelf loadDataWithYear:self.year];
    }];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.refresh=YES;
        weakSelf.page=0;
        [weakSelf loadListData];
        
    }];
    
  
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mediaTypeAction:) name:@"mediaType" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(priceModelAction:) name:@"priceModel" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addressAction:) name:@"address" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(mediaTypeCancelAction:) name:@"mediaTypeCancel" object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addressCancelAction:) name:@"addressCancel" object:nil];
    
    
}
-(void)addressCancelAction:(NSNotification *)noc
{
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    [dataArray addObjectsFromArray:self.collArray];
    for (NSObject *objC in dataArray) {
        if ([objC isKindOfClass:[WYCity class]]) {
            [self.collArray removeObject:objC];
        }
        
        
    }
    [self clickSureAction];
}
-(void)addressAction:(NSNotification *)noc
{
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    [dataArray addObjectsFromArray:self.collArray];
    for (NSObject *objC in dataArray) {
        if ([objC isKindOfClass:[WYCity class]]) {
            [self.collArray removeObject:objC];
        }
    }
    [self.collArray addObjectsFromArray:noc.object[@"address"]];
    [self clickSureAction];
}
-(void)priceModelAction:(NSNotification *)noc
{
    [self.collArray addObjectsFromArray:noc.object[@"priceModel"]];
    [self clickSureAction];
}
-(void)mediaTypeAction:(NSNotification *)noc
{
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    [dataArray addObjectsFromArray:self.collArray];
    for (NSObject *objC in dataArray) {
        if ([objC isKindOfClass:[WYUpModel class]]) {
            [self.collArray removeObject:objC];
        }
        if ([objC isKindOfClass:[WYAttribute class]]) {
            [self.collArray removeObject:objC];
        }
        
        
    }
    
    if (noc.object[@"upModel"]!=nil) {
       [self.collArray addObject:noc.object[@"upModel"]];
    }
    if (noc.object[@"detail"]!=nil) {
        [self.collArray addObjectsFromArray:noc.object[@"detail"]];
    }
    if (self.typeId!=nil) {
        self.typeId = noc.object[@"typeID"];
    }
    [self clickSureAction];
}
-(void)mediaTypeCancelAction:(NSNotification *)noc
{
    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
    [dataArray addObjectsFromArray:self.collArray];
    for (NSObject *objC in dataArray) {
        if ([objC isKindOfClass:[WYUpModel class]]) {
            [self.collArray removeObject:objC];
        }
        if ([objC isKindOfClass:[WYAttribute class]]) {
            [self.collArray removeObject:objC];
        }
        
        
    }
    
    [self clickSureAction];
}

-(void)clickHotMedia:(NSNotification *)notification
{
    [self.tabBarController setSelectedIndex:1];
    NSString *key =notification.object[@"key"];
    if (key.length) {
        self.textField.text = key;
    }
}
-(void)addNoticfic:(NSNotification *)noc
{
    [self.collArray removeAllObjects];
    [self.tabBarController setSelectedIndex:1];
    NSDictionary *dic = noc.object;
    NSArray *array = dic.allKeys;
    
    WYMediaType *type = dic.allValues[0];
    
    self.isNoc = YES;
    self.titleView.isNoction = YES;
    
    
    WYUpModel *model = [[WYUpModel alloc]init];
    model.id = type.id;
    self.typeId  = type.id;
    model.name = type.mediaTypeName;
    
    
    if ([array[0] intValue]==9) {
        NSLog(@"9999999999999");
    }else{
          NSLog(@"777777777777");
        self.NocmediaType = type;
        [self.collArray addObject:model];
      //  [self getAttrByTypeId:type.id];
    }
    [self  clickSureAction];
}

-(void)loadListData
{
//   // NSDictionary *dic = @{
//        @"maps":@[@{@"attributeValueId":@[@"541",@"343",@"344"]} ]
//                          };
    //NSString *searchParm = [self convertToJsonData:dic];
    
    NSMutableDictionary *parm = [[NSMutableDictionary alloc]init];
    
    if (self.typeId.length) {
        parm[@"typeId"]=self.typeId;
    }
    
   
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    NSMutableArray *array = [[NSMutableArray alloc]init];
    for (WYAttribute *attr in self.collArray) {
        if (attr.id) {
            [array addObject:attr.id];
        }
        
    }
    dic[@"attributeValueId"]=array;
    parm[@"maps"]=@[dic];
    
    parm[@"area"]=self.leftButton.currentTitle;
    if (self.hot== WYHotNormal) {
        
    }
    if (self.hot == WYHotHigh) {
        parm[@"heat"]=@"0";
    }
    if (self.hot == WYHotLow) {
        parm[@"heat"]=@"1";
    }
    if (self.keyWord.length) {
        parm[@"keyword"]=self.keyWord;
    }
    
    for (NSObject *object in self.collArray) {
        if ([object isKindOfClass:[WYPriceModel class]]) {
            WYPriceModel *price = (WYPriceModel *)object;
            if (price.maxPrice.length) {
                parm[@"maxPrice"]=price.maxPrice;
            }
            if (price.minPrice.length) {
                parm[@"minPrice"]=price.minPrice;
            }
        }
        
        
    }
    
    
    
    
    
   // parm[@"typeId"]=@"7";
    
    
    
    
    NSString *searchParm = [self convertToJsonData:parm];
    //NSLog(@"%@",searchParm);
    WEAKSELF
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    [WYHttpServer getMediaListWithSearchParam:searchParm WithPageNum:page WithPageSize:@"10" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            
            if (weakSelf.refresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            
            
            if ([WYMedia getMediaListFromJson:response[@"result"]].count) {
                [weakSelf.dataArray addObject:[WYMedia getMediaListFromJson:response[@"result"]]];
                
            }
            [weakSelf.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        }
        
    }];
    
    
    
    
}
-(NSString *)convertToJsonData:(NSDictionary *)dict

{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden=YES;
}
-(void)setupUI
{
    [self.view addSubview:self.tableView];
    WEAKSELF
   

  //  view.collectionView.allowsMultipleSelection =YES;
    
    UIView *categoryView = [[UIView alloc]init];
    self.categoryView = categoryView;
    [self.view addSubview:categoryView];
    [categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0);
        make.top.mas_equalTo(NavHeight+44);
    }];
    WYConditionView *conditionView = [[WYConditionView alloc]init];
    self.conditionView = conditionView;
    [categoryView addSubview:conditionView];
//    [conditionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.mas_equalTo(0);
//        make.height.mas_equalTo(44);
//    }];
    conditionView.frame  = CGRectMake(0, 0, kScreenWidth, 0);
    
    
    [conditionView.collectionView registerNib:[UINib nibWithNibName:@"WYMdeiaCell" bundle:nil] forCellWithReuseIdentifier:@"WYMdeiaCell"];
    conditionView.collectionView.dataSource = self;
 //   conditionView.collectionView.delegate = self;
    conditionView.collectionView.backgroundColor =[UIColor whiteColor];
    conditionView.resetButton.hidden = YES;
    [conditionView.resetButton addTarget:self action:@selector(clickResetAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
       // make.height.mas_equalTo(kScreenHeight-88);
        make.top.equalTo(categoryView.mas_bottom).offset(0);
        if (kDevice_Is_iPhoneX) {
            make.bottom.mas_equalTo(0);
        }else{
            make.bottom.mas_equalTo(0);
        }
        
    }];
    
    
    [self addLeftButtonText:@"北京市" width:0 height:0];
    [self.leftButton setImage:[UIImage imageNamed:@"icon_down_arrow"] forState:(UIControlStateNormal)];
    
    self.leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0);
    self.leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 60, 0, 0);
    self.leftButton.frame = CGRectMake(1, kDevice_Is_iPhoneX?44:20, 70, 44);
    UIView *titleView = [[UIView alloc] init];
    if (kDevice_Is_iPhoneX) {
        titleView.frame = CGRectMake(76, 44, kScreenWidth-88, 44);
    }else{
        titleView.frame = CGRectMake(76, 20, kScreenWidth-88, 44);
    }
    
    UITextField *textField = [[UITextField alloc]init];
    self.textField = textField;
    [titleView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        
    }];
    textField.backgroundColor = [UIColor hexStringToColor:@"#fafafa" alpha:1.0f];
    textField.placeholder = @"请输入你要了解的内容";
    textField.borderStyle = UITextBorderStyleNone;
    textField.returnKeyType = UIReturnKeySearch;
    textField.delegate = self;
    [self initTopBarWithView:titleView];
    //self.topBar.frame = CGRectMake(70+15, 0, kScreenWidth-90, NavHeight);
   // [textField setBorderWithWidth:kLineHeight andColor:[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0]];
    [textField setCornerRadius:5.0f];
    
    UIView *leftView = [[UIView alloc]init];
    leftView.frame = CGRectMake(0, 0, 28, 34);
    
    UIImageView *imageView = [[UIImageView alloc]init];
    [leftView addSubview:imageView];
    imageView.frame = CGRectMake(5, 6, 22, 22);
    imageView.image = [UIImage imageNamed:@"search-1"];
    imageView.tintColor = [UIColor blackColor];
    textField.leftView = leftView;
    textField.leftView.tintColor = [UIColor hexStringToColor:@"#333333" alpha:1.0];
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    WYMediaTitleView *view = [[WYMediaTitleView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    self.titleView = view;
    view.frame = CGRectMake(0, NavHeight, kScreenWidth, 44);
    [self.view addSubview:view];
    view.complete = ^(NSUInteger index) {
        NSLog(@"%ld",index);
        _index = index;
        if (index==1) {
            // self.tvc.view.hidden = !self.tvc.view.isHidden;
        }
        if (index==3) {
            
        }
        if (index==4) {
            
        }
    };
    view.delegate=self;
  
   
    
    
    
    
    
}
-(void)leftButtonPress
{
    TLCityPickerController *cityPickerVC = [[TLCityPickerController alloc] init];
    [cityPickerVC setDelegate:self];
    
    // 设置定位城市
   // cityPickerVC.locationCityID = @"1400010000";
    
    // 最近访问城市，如果不设置，将自动管理
    //  cityPickerVC.commonCitys = [[NSMutableArray alloc] initWithArray: @[@"1400010000", @"100010000"]];
    
    // 热门城市，需手动设置
    cityPickerVC.hotCitys = @[@"100010000", @"200010000", @"300210000", @"600010000", @"300110000"];
//    self.navigationController.navigationBar.hidden = NO;
    // 支持push、present方式跳入，但需要有UINavigationController
    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:cityPickerVC] animated:YES completion:^{
        
        
    }];
}

#pragma mark - TLCityPickerDelegate
- (void) cityPickerController:(TLCityPickerController *)cityPickerViewController didSelectCity:(TLCity *)city
{
    WEAKSELF
    [self.leftButton setTitle:city.cityName forState:UIControlStateNormal];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeCity" object:@{@"city":city.cityName}];
    self.refresh = YES;
    [self loadListData];
    
    // self.navigationItem.leftBarButtonItem.title=city.cityName;
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
        //[weakSelf loadCityList];
    }];
}
- (void) cityPickerControllerDidCancel:(TLCityPickerController *)cityPickerViewController
{
    [cityPickerViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    self.keyWord = textField.text;
    WYSearchReusltController *src = [[WYSearchReusltController alloc]init];
    src.searchTitle = textField.text;
    [self.navigationController pushViewController:src animated:YES];
    return YES;
}

-(void)clickResetAction:(UIButton *)button
{
    self.NocmediaType.id=@"";
    self.typeId = @"";
    [self.collArray removeAllObjects];
    [self clickSureAction];
}


-(void)clickCancelAction
{
    for (int i = (int)self.collArray.count-1; i>=0; i--) {
        if (_index==1) {
            self.typeId = @"";
            [self.titleView setHeadDataArray:nil];
            if ([self.collArray[i] isKindOfClass:[WYAttribute class]]) {
                WYAttribute *attr = self.collArray[i];
                [self.collArray removeObject:attr];
            }
           
        }
        
    }
    
    for (int i =(int) self.collArray.count-1; i>=0; i--) {
        if (_index==1) {
         
            if ([self.collArray[i] isKindOfClass:[WYUpModel class]]) {
                [self.collArray removeObject:self.collArray[i]];
            }
            
        }
       
        
    }
    
    if (_index==4) {
        [self.titleView setCityArray:nil];
        for (int i =(int) self.collArray.count-1; i>=0; i--) {
            if ([self.collArray[i] isKindOfClass:[WYCity class]]) {
                [self.collArray removeObjectAtIndex:i];
            }
        }
    }
    
    if (self.collArray.count) {
        
    }else{
        [self.categoryView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(0);
            make.top.equalTo(self.titleView.mas_bottom).offset(0);
        }];
        self.conditionView.resetButton.hidden = YES;
    }
    [self.conditionView.collectionView reloadData];
    self.refresh = YES;
    [self loadListData];
 //   [self.collArray removeAllObjects];
    NSLog(@"点击了取消的按钮--%ld",_index);
}

-(void)clickSureAction
{
   
    if (self.collArray.count) {
//        [self.categoryView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.mas_equalTo(0);
//            make.height.mas_equalTo(44);
//            make.top.equalTo(self.titleView.mas_bottom).offset(0);
//        }];
        [self.categoryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(44);
        }];
        self.conditionView.frame  = CGRectMake(0, 0, kScreenWidth, 44);
        self.conditionView.resetButton.hidden = NO;
        self.refresh = YES;
        [self loadListData];
    }else{
//        [self.categoryView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.right.mas_equalTo(0);
//            make.height.mas_equalTo(0);
//            make.top.equalTo(self.titleView.mas_bottom).offset(0);
//        }];
        [self.categoryView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        self.conditionView.frame  = CGRectMake(0, 0, kScreenWidth, 0);
        self.conditionView.resetButton.hidden = YES;
        self.refresh = YES;
        [self loadListData];
    }
     [self.conditionView.collectionView reloadData];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  [self.dataArray[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WYRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYRecommendCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"WYRecommendCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    WYMedia *media = self.dataArray[indexPath.section][indexPath.row];
    [cell setMedia:media];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYRecommendCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    WYMediaDetailController *mdc = [[WYMediaDetailController alloc]init];
    WYMedia *media = self.dataArray[indexPath.section][indexPath.row];
    mdc.ID = media.id;
    mdc.image = cell.companyImageView.image;
    mdc.media = media;
    [self.navigationController pushViewController:mdc animated:YES];
    
  //  NSLog(@"%ld---%ld",indexPath.section,indexPath.row);
}






-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [_tableView registerNib:[UINib nibWithNibName:@"WYRecommendCell" bundle:nil] forCellReuseIdentifier:@"WYRecommendCell"];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

#pragma mark collectionView
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.titleView.collectionView||collectionView==self.titleView.cityCollectionView||collectionView==self.titleView.priceCollectionView) {
        if (_index==1) {
            if (indexPath.section==0) {
                if (self.collArray.count) {
                   
                    for (NSUInteger i=self.collArray.count; i > 0; i--) {
                        
                        NSLog(@"i=============%lu",(unsigned long)i);
                        
                        //NSLog(@"%d---%@",i,self.collArray[i]);
                        if ([self.collArray[i-1] isKindOfClass:[WYAttribute class]]) {
                            WYAttribute *attr = self.collArray[i-1];
                            [self.collArray removeObject:attr];
                        }
                    }
                    for (NSUInteger i=self.collArray.count; i > 0; i--) {
                        if ([self.collArray[i-1] isKindOfClass:[WYUpModel class]]) {
                            [self.collArray removeObject:self.collArray[i-1]];
                        }
                    }
                    
                    
                    
                    
                    
                }
               
//
//                self.typeId = type.id;
//
//                WYUpModel *upmodel = [[WYUpModel alloc]init];
//                upmodel.id = type.id;
//                upmodel.name = type.mediaTypeName;
//                [self.collArray addObject:upmodel];
                [self.conditionView.collectionView reloadData];
            }else{
                WYMdeiaCell *cell = (WYMdeiaCell *)[collectionView cellForItemAtIndexPath:indexPath];
                WYAttrList *list = self.headDataArray[indexPath.section];
                WYAttribute *attr = list.list[indexPath.row];
                if (attr.state) {
                    [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
                    [cell setBackgroundColor:[UIColor whiteColor]];
                    attr.state = NO;
                    [self.collArray removeObject:attr];
                }else{
                    [cell setBorderWithWidth:kLineHeight andColor:RGBA(250, 165, 122, 1.0)];
                    [cell setBackgroundColor:RGBA(255, 247, 244, 1.0)];
                    attr.state = YES;
                    [self.collArray addObject:attr];
                }
            }
        }
        if (_index==3) {
            self.titleView.priceRangeCell.leftTextfield.text=@"";
            self.titleView.priceRangeCell.rightTextfield.text=@"";
            if (self.collArray.count) {
                for (NSUInteger i = self.collArray.count; i>0; i--) {
                    if ([self.collArray[i-1] isKindOfClass:[WYPriceModel class]]) {
                        [self.collArray removeObjectAtIndex:i-1];
                    }
                }
            }
       //     WYMdeiaCell *cell = (WYMdeiaCell *)[collectionView cellForItemAtIndexPath:indexPath];
            for (WYPriceModel *model in self.priceArray) {
                model.state = NO;
            }
            WYPriceModel *model = self.priceArray[indexPath.row];
            model.state = YES;
            [collectionView reloadData];
            
            if (indexPath.row!=0) {
                 [self.collArray addObject:model];
            }
           
        }
        if (_index==4) {
          //  WYMdeiaCell *cell = (WYMdeiaCell *)[collectionView cellForItemAtIndexPath:indexPath];
            if (indexPath.row==0) {
                for (WYCity *city in self.cityArray) {
                    city.isState = NO;
                }
                for (NSUInteger i = self.collArray.count; i>0; i--) {
                    if ([self.collArray[i-1] isKindOfClass:[WYCity class]]) {
                        [self.collArray removeObjectAtIndex:i-1];
                    }
                }
                
            }else{
                WYCity *city = self.cityArray[0];
                city.isState = NO;
                
                for (NSUInteger i = self.collArray.count; i>0; i--) {
                    if ([self.collArray[i-1] isKindOfClass:[WYCity class]]) {
                        WYCity *city = self.collArray[i-1];
                        if ([city.name isEqualToString:@"全部"]) {
                            [self.collArray removeObjectAtIndex:i-1];
                        }
                    }
                }
                
            }
            WYCity *city =self.cityArray[indexPath.row];
            city.isState = !city.isState;
            if (city.isState) {
                [self.collArray addObject:city];
            }else{
                [self.collArray removeObject:city];
            }
            [collectionView reloadData];
        }
    }
}

#pragma mark - collectionview

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WYMdeiaCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYMdeiaCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYMdeiaCell" owner:nil options:nil].firstObject;
    }
    [cell setBorderWithWidth:kLineHeight andColor:[UIColor grayColor]];
    [cell setCornerRadius:5.0];
    
    if ([self.collArray[indexPath.row] isKindOfClass:[WYAttribute class]] ) {
        WYAttribute *attr = self.collArray[indexPath.row];
        cell.textLabel.text=attr.mediaTypeName;
    }
    if ([self.collArray[indexPath.row] isKindOfClass:[WYUpModel class]] ) {
        WYUpModel *attr = self.collArray[indexPath.row];
        cell.textLabel.text=attr.name;
    }
    if ([self.collArray[indexPath.row] isKindOfClass:[WYPriceModel class] ]) {
        WYPriceModel *model = self.collArray[indexPath.row];
        cell.textLabel.text = model.name;
    }
    if ([self.collArray[indexPath.row] isKindOfClass:[WYCity class] ]) {
        WYCity *model = self.collArray[indexPath.row];
        cell.textLabel.text = model.name;
    }
    return cell;
}

-(void)inputPriceinConditionborderAndType:(BOOL)type WithMaxPrice:(NSString *)maxPrice WithMinPrice:(NSString *)minPrice
{
    for (WYPriceModel *model in self.priceArray) {
        model.state= NO;
    }
    [self.titleView.priceCollectionView reloadData];
    if (type) {
        if (minPrice.integerValue>maxPrice.integerValue) {
            
            [self toast:@"最大范围不能小于最小范围"];
            
        }
        WYPriceModel *model = [[WYPriceModel alloc]init];
        model.name = [NSString stringWithFormat:@"%@~%@万",minPrice,maxPrice];
        model.minPrice = minPrice;
        model.maxPrice = maxPrice;
        
        for (NSUInteger i = self.collArray.count; i>0; i--) {
            if ([self.collArray[i-1] isKindOfClass:[WYPriceModel class]]) {
                [self.collArray removeObjectAtIndex:i-1];
            }
        }
        
        [self.collArray addObject:model];
        
        
        
    }else{
        
        WYPriceModel *model = [[WYPriceModel alloc]init];
        if (maxPrice.length) {
           model.name = [NSString stringWithFormat:@"%@万以下",maxPrice];
        }
        if (minPrice.length) {
            model.name = [NSString stringWithFormat:@"%@万以上",minPrice];
        }
        model.minPrice = minPrice;
        model.maxPrice = maxPrice;
        
        for (NSUInteger i = self.collArray.count; i>0; i--) {
            if ([self.collArray[i-1] isKindOfClass:[WYPriceModel class]]) {
                [self.collArray removeObjectAtIndex:i-1];
            }
        }
        [self.collArray addObject:model];
        
        
    }
}

-(void)clickHotTypeWithType:(WYHot)hotType
{
    self.hot = hotType;
    self.refresh = YES;
    [self loadListData];
}










@end
